package com.seres.util;

import cn.hutool.core.thread.ExecutorBuilder;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @Description : 线程池
 * @ClassName : ThreadPool
 * @Author : 程磊
 * @Date: 2020-06-24 10:00
 */
@Component
@Slf4j
@Getter
public class ThreadPool implements DisposableBean {

    private ExecutorService executor;

    /**核心线程数*/
    @Value("${thread.pool.corePoolSize:32}")
    private int corePoolSize;

    /**最大线程数*/
    @Value("${thread.pool.maxPoolSize:128}")
    private int maxPoolSize;

    /**线程存货时间*/
    @Value("${thread.pool.keepAliveTime:30}")
    private long keepAliveTime;

    @PostConstruct
    void init(){
        ExecutorBuilder builder = ExecutorBuilder.create()
                .setCorePoolSize(corePoolSize)
                .setMaxPoolSize(maxPoolSize)
                .setKeepAliveTime(keepAliveTime, TimeUnit.SECONDS)
                .setWorkQueue(new LinkedBlockingQueue());

        executor = builder.build();
        log.info("创建线程池成功：corePoolSize={},maxPoolSize={},keepAliveTime={}s",corePoolSize,maxPoolSize,keepAliveTime);
    }
    
    /**
     * @description 执行线程 
     * @param runnable: 
     * @author 程磊
     * @Date 2020/6/24 10:23 
     * @return: void
     */
    public void execute(Runnable runnable){
        executor.execute(runnable);
    }

    /**
     * @description 执行线程获得一个回调结果
     * @param t: 
     * @author 程磊
     * @Date 2020/6/24 10:23 
     * @return: java.util.concurrent.Future<T>
     */
    public <T> Future<T> submit(Callable<T> t){
        return executor.submit(t);
    }

    public void shutdown(boolean isNow) {
        if (null != executor) {
            if (isNow) {
                log.info("暴力关闭线程池。。。");
                executor.shutdownNow();
            } else {
                log.info("优雅的关闭线程池。。。");
                executor.shutdown();
            }
        }
    }


    @Override
    public void destroy() throws Exception {
        shutdown(false);
    }
}
